package com.hamizhong.iflowers.web.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hamizhong.iflowers.service.ICommentService;
import com.hamizhong.iflowers.service.IGoodsService;
import com.hamizhong.iflowers.utils.MyConstants;
import com.hamizhong.iflowers.web.base.MyBaseAjaxAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.CommentVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;

public class AjaxQueryCommentsByGoodsAction extends MyBaseAjaxAction {
	private IGoodsService iGoodsService = null;
	private ICommentService iCommentService = null;
	
	public IGoodsService getiGoodsService() {
		return iGoodsService;
	}
	public void setiGoodsService(IGoodsService iGoodsService) {
		this.iGoodsService = iGoodsService;
	}
	public ICommentService getiCommentService() {
		return iCommentService;
	}
	public void setiCommentService(ICommentService iCommentService) {
		this.iCommentService = iCommentService;
	}

	@Override
	protected JSONObject doRequest() {
		logger.info("...invoke...AjaxQueryCommentsByGoodsAction...");
		String goodsId = getRQ().getParameter("goodsId");
		String pageNum = getRQ().getParameter("pageNum");
		int pageSize = MyConstants.PAGE_SIZE_COMMON_CLIENT_ORDERS;
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		JSONObject jobj = null;
		try {
			IOPResultDto<IObjectVO> dto = iCommentService.findByGoodsId(Integer.valueOf(goodsId), Integer.valueOf(pageNum), pageSize);
			if(dto.isSuccess()) {
				List<CommentVO> commentVOList = dto.getFirstResult().getCommentVOList();
				for (CommentVO commentVO : commentVOList) {
					jobj = new JSONObject();
					jobj.put("commentId", commentVO.getCommentId());
					jobj.put("userName", commentVO.getUserName());
					jobj.put("comment", commentVO.getComment());
					jobj.put("createDateStr", commentVO.getCreateDateStr());
					ja.add(jobj);
				}
				jo.put("data", ja);
				jo.put("result", MyConstants.AJAX_SUCCESS);
			}else {
				jo.put("result", MyConstants.AJAX_FAILED_ALERT);
				jo.put("msg", dto.getErrorMSG());
			}
		} catch (Exception e) {
			e.printStackTrace();
			jo.put("result", MyConstants.AJAX_FAILED_ALERT);
			jo.put("msg", "初始化商品评论失败！");
		}
		return jo;
	}

}
